package yxy.game.pm2.command.worldboss;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankWorship;
import yxy.game.pm2.bean.module.rank.RankWorshipPlayer;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.WorldBossMessage;
import yxy.game.pm2.module.player.PlayerHandle;
import yxy.game.pm2.module.worldboss.RankWorldBoss;
import yxy.game.pm2.module.worldboss.WorldBossHandle;

import java.util.Collection;

/** 世界BOSS 世界BOSS排行榜 */
@MessageType(proto = Command.WorldBossRank, classtype = WorldBossMessage.WorldBossRankRequest.class)
public class CMD_WorldBossRank extends Command<WorldBossMessage.WorldBossRankRequest> {

	@Override
	public void execute(MessageSource messageSource, WorldBossMessage.WorldBossRankRequest request) {
		String playerCode = messageSource.getTag();

		WorldBossMessage.WorldBossRankResponse.Builder response = WorldBossMessage.WorldBossRankResponse.newBuilder();

		// 判断世界等级
		WorldBossHandle bossHandle = new WorldBossHandle();
		AreaBean areaBean = AreaModule.area(playerCode);
		Result result = bossHandle.checkWorldLevel(areaBean);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}

		RankWorldBoss rankWorldBoss = new RankWorldBoss(areaBean);
		try {
			PlayerHandle playerHandle = new PlayerHandle();
			RankWorship worship = rankWorldBoss.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			Collection<RankEntry> top = rankWorldBoss.top(rankWorldBoss.topSize());
			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();
				long damage = entry.getValue();

				WorldBossMessage.RankWorldBoss.Builder builder = WorldBossMessage.RankWorldBoss.newBuilder();
				builder.setRank(++rank);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setDamage(damage);
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}

			{
				int playerRank = 0;
				long damage = 0;
				RankEntry entry = rankWorldBoss.get(playerCode);
				if (entry != null) {
					playerRank = rankWorldBoss.rank(playerCode);
					damage = entry.getValue();
				}

				WorldBossMessage.RankWorldBoss.Builder builder = WorldBossMessage.RankWorldBoss.newBuilder();
				builder.setRank(playerRank);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
				builder.setDamage(damage);
				response.setMy(builder);
			}

			response.setTimes(worshipPlayer.remainTimes());

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}

	}

	private void send(MessageSource messageSource, WorldBossMessage.WorldBossRankResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}